import Blockly from 'blockly/core'
import 'blockly/python_compressed'
import 'blockly/blockly_compressed'
import 'blockly/blocks_compressed'

Blockly.Blocks.class = {
  init: function () {
    this.appendDummyInput()
      .appendField('define class')
      .appendField(new Blockly.FieldTextInput('myClass'), 'class-name')
    this.appendStatementInput('variables')
      .setCheck('Variable')
      .appendField('variables')
    this.appendStatementInput('methods')
      .setCheck('Method')
      .appendField('methods')
    this.setInputsInline(false)
    this.setColour(15)
    this.setTooltip('pass关键词')
    this.setHelpUrl('')
  }
}

Blockly.Python.class = function (block) {
  const textClassName = block.getFieldValue('class-name')
  const statementsVariables = Blockly.Python.statementToCode(
    block,
    'variables'
  )
  const statementsMethods = Blockly.Python.statementToCode(block, 'methods')
  let code = 'class ' + textClassName + ':' + '\n'
  code += statementsVariables
  code += statementsMethods
  return code
}

Blockly.Blocks.variable = {
  init: function () {
    this.appendValueInput('Variables')
      .setCheck(null)
      .appendField(new Blockly.FieldTextInput('valueName'), 'valueName')
      .appendField('=')
    this.setInputsInline(true)
    this.setPreviousStatement(true, 'Variable')
    this.setNextStatement(true, 'Variable')
    this.setColour(15)
    this.setTooltip('与class配套')
    this.setHelpUrl('')
  }
}

Blockly.Python.variable = function (block) {
  const textName = block.getFieldValue('valueName')
  const valueVariables = Blockly.Python.valueToCode(
    block,
    'Variables',
    Blockly.Python.ORDER_NONE
  )
  const code = textName + ' = ' + valueVariables + '\n'
  return code
}

export default Blockly
